package com.lg.partition27;

import java.io.*;
import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2022/4/21 17:51
 */
public class P2661NOIP2015提高组_信息传递_tarjan {

    private static final int N = 200010, M = N;
    private static int[] h = new int[N], e = new int[M], ne = new int[M];
    private static int[] dfn = new int[N], low = new int[N], scc = new int[N], size = new int[N], stk = new int[N];
    private static boolean[] instk = new boolean[N];
    private static int n, idx = 0, timestamp = 0, top = 0, sccId = 0;

    private static StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    private static PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        n = nextInt();
        Arrays.fill(h, -1);
        for (int i = 1; i <= n; i++) {
            int j = nextInt();
            add(i, j);
        }
        //tarjan找有向图的最小环
        for (int i = 1; i <= n; i++) {
            if (dfn[i] == 0) tarjan(i);
        }
        int ans = 0x3f3f3f3f;
        for (int i = 1; i <= sccId; i++) {
            if (size[i] > 1) {
                ans = Math.min(ans, size[i]);
            }
        }
        writer.println(ans);
        writer.flush();
    }

    private static void tarjan(int u) {
        dfn[u] = low[u] = ++timestamp;
        stk[++top] = u;
        instk[u] = true;
        for (int i = h[u]; i != -1; i = ne[i]) {
            int v = e[i];
            if (dfn[v] == 0) {
                tarjan(v);
                low[u] = Math.min(low[u], low[v]);
            } else if (instk[v]) {
                low[u] = Math.min(low[u], dfn[v]);
            }
        }
        if (dfn[u] == low[u]) {
            sccId++;
            int v = -1;
            do {
                v = stk[top--];
                instk[v] = false;
                scc[v] = sccId;
                size[sccId]++;
            } while (v != u);
        }
    }

    private static void add(int a, int b) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
    }

    private static int nextInt() throws IOException {
        tokenizer.nextToken();
        return (int) tokenizer.nval;
    }
}
